clc;
clear all;

%读取数据，能显示每帧数据
%%% This script is used to read the binary file produced by the DCA1000
%%% and Mmwave Studio
%%% Command to run in Matlab GUI -
%readDCA1000('<ADC capture bin file>') function [retVal] = readDCA1000(fileName)
%% global variables
% change based on sensor config
numADCSamples = 256; % number of ADC samples per chirp
numADCBits = 16; % number of ADC bits per sample
numRX = 4; % number of receivers
numLanes = 2; % do not change. number of lanes is always 2
isReal = 0; % set to 1 if real only data, 0 if complex data0
n_chirps=128;%每一帧中chirps数，no of chirp loops 
frame=1;%第多少帧

global c B K T Tc fs f0 lambda d
c=3.0e8;  
B=768e6;       %调频带宽
K=29.982e12;       %调频斜率
T=B/K;         %调频周期
Tc=80e-6;     %chirp总周期
fs=1e7;       %采样率
f0=77e9;       %初始频率
lambda=c/f0;   %雷达信号波长
d=lambda/2;    %天线阵列间距

N = 256;       %距离向FFT点数
M = 128;       %多普勒向FFT点数
Q = 180;       %角度FFT
max_range = c * fs / (2 * B); % 最大距离
max_speed = lambda / (4 * Tc); % 最大速度
%% read file
% read .bin file
fileName='D:\20241022\20241110\1.bin';
fid = fopen(fileName,'r');
adcData = fread(fid, 'int16');
% if 12 or 14 bits ADC per sample compensate for sign extension
if numADCBits ~= 16
    l_max = 2^(numADCBits-1)-1;
    adcData(adcData > l_max) = adcData(adcData > l_max) - 2^numADCBits;
end
fclose(fid);
fileSize = size(adcData, 1);
% real data reshape, filesize = numADCSamples*numChirps
if isReal
    numChirps = fileSize/numADCSamples/numRX;
    LVDS = zeros(1, fileSize);
    %create column for each chirp
    LVDS = reshape(adcData, numADCSamples*numRX, numChirps);
    %each row is data from one chirp
    LVDS = LVDS.';
else
    % for complex data
    % filesize = 2 * numADCSamples*numChirps
    numChirps = fileSize/2/numADCSamples/numRX;%总chirps数
    numframe=numChirps/n_chirps;%总帧数 no of frames，总chirps数除以每帧chirps数。
    LVDS = zeros(1, fileSize/2);
    %combine real and imaginary part into complex data
    %read in file: 2I is followed by 2Q
    counter = 1;
    for i=1:4:fileSize-1
        LVDS(1,counter) = adcData(i) + sqrt(-1)*adcData(i+2); 
        LVDS(1,counter+1) = adcData(i+1)+sqrt(-1)*adcData(i+3); 
        counter = counter + 2;
    end
        % create column for each chirp
        LVDS = reshape(LVDS, numADCSamples*numRX, numChirps);
        %each row is data from one chirp
        LVDS = LVDS.';
end
%organize data per RX
adcData = zeros(numRX,numChirps*numADCSamples);
for row = 1:numRX
    for i = 1: numChirps
        adcData(row, (i-1)*numADCSamples+1:i*numADCSamples) = LVDS(i, (row-1)*numADCSamples+1:row*numADCSamples);
    end
end
% return receiver data
adcData;
%radar_data = reshape(adcData,numADCSamples, numChirps, numframe, numRX);
data = reshape(adcData,numADCSamples,n_chirps,numframe,numRX);
data1 = data(:,:,1,1);
data2 = data(:,:,2,1);
data3 = data(:,:,3,1);
data4 = data(:,:,4,1);
% 对所有 Chirp 和接收通道执行距离 FFT
rangeFFT = fft(data, N, 1); % N 为距离 FFT 点数
% 对距离 FFT 结果进行多普勒 FFT
dopplerFFT = fft(rangeFFT, M, 2); % M 为速度 FFT 点数
% 对速度 FFT 结果进行角度 FFT
angleFFT = fft(dopplerFFT, Q, 3); % Q 为角度 FFT 点数
% 计算距离、速度和角度

        figure;
        rangeDoppler = reshape(dopplerFFT(:,:,1), N, M);
        rangeDoppler_Temp = rangeDoppler';
        
        % 距离轴和多普勒轴的向量
        range_vec = linspace(0, (N-1)*fs*c/N/2/K, N); % 距离向量
        doppler_vec = linspace(-M/2, M/2-1, M) * lambda / Tc / M; % 多普勒向量
        
        % 计算幅度
        Z = abs(rangeDoppler_Temp);
        
        % 绘制多普勒-距离图
        imagesc(doppler_vec, range_vec, Z); % 显示带色彩的二维图像
        % colormap(hot); % 设置颜色条
        % colorbar;
        xlabel('多普勒速度 (m/s)');
        ylabel('距离 (m)');
        title(sprintf('2维FFT视图 - 多普勒-距离图, 第 %d 帧', 1));
        ylim([0 (N-1)*fs*c/N/2/K]);
        xlim([(-M/2)*lambda/Tc/M (M/2-1)*lambda/Tc/M]);